package com.yk.i_wms.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.mapper.PdtWorkOrderMapper;
import com.yk.i_wms.model.PdtBom;
import com.yk.i_wms.model.PdtReplaceItem;
import com.yk.i_wms.model.PdtWorkOrder;
import com.yk.i_wms.service.IPdtBomService;
import com.yk.i_wms.service.IPdtReplaceItemService;
import com.yk.i_wms.service.IPdtWorkOrderService;
import com.yk.i_wms.vo.BomVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 工单号
 *
 * @author hbq
 * @date 2021-04-01 13:45:28
 */
@Slf4j
@Service
public class PdtWorkOrderServiceImpl extends ServiceImpl<PdtWorkOrderMapper, PdtWorkOrder> implements IPdtWorkOrderService {
    @Resource
    private PdtWorkOrderMapper pdtWorkOrderMapper;
    @Autowired
    private IPdtBomService pdtBomService;
    @Autowired
    private IPdtReplaceItemService pdtReplaceItemService;
    /**
     * 列表
     * @param params
     * @return
     */
    public Page<Map> findList(Map<String, Object> params){
        Integer pageNum = MapUtils.getInteger(params, "pageNum");
        Integer pageSize = MapUtils.getInteger(params, "pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = -1;
        }
        Page<Map> pages = new Page<>(pageNum, pageSize);
        return pdtWorkOrderMapper.findList(pages, params);
    }

    @Override
    public Result getItemListByMoNo(Map<String, Object> params) {
        String woNo = MapUtil.getStr(params, "woNo");
        PdtWorkOrder pdtWorkOrder = pdtWorkOrderMapper.selectOne(new LambdaQueryWrapper<PdtWorkOrder>().eq(PdtWorkOrder::getWoNo, woNo));
        if (pdtWorkOrder == null) {
          return  Result.failed("无此工单");
        }
        String modelCode = pdtWorkOrder.getModelCode();
        Long planQty = pdtWorkOrder.getPlanQty();
        List<PdtBom> minSonList = pdtBomService.getMinSonList(modelCode);
        List<BomVO> bomVOS=new ArrayList<>();
        for (PdtBom pdtBom : minSonList) {
            PdtReplaceItem replaceItem = pdtReplaceItemService.getOne(new LambdaQueryWrapper<PdtReplaceItem>()
                    .eq(PdtReplaceItem::getItemCode, pdtBom.getItemCode())
                    .eq(PdtReplaceItem::getIsOpen, 1)
            );
            BomVO bomVO = BomVO.builder()
                    .itemCode(pdtBom.getItemCode())
                    .itemName(pdtBom.getItemName())
                    .modelCode(pdtWorkOrder.getModelCode())
                    .modelName(pdtWorkOrder.getModelName())
                    .num(planQty * pdtBom.getUseNum())
                    .replaceGroup(replaceItem==null?"":replaceItem.getReplaceGroup())
                    .woNo(woNo)
                    .build();
            bomVOS.add(bomVO);
        }
        return Result.succeed(bomVOS,"查询成功");
    }
}
